#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=50
USE_PROCD=1

target_conf_dir="/var/nps/conf/"
target_conf_file="nps.conf"
nps_def_conf_path="/etc/nps/conf/nps.conf"

target_conf_path="$target_conf_dir$target_conf_file"

export Enabled 
export log_level log_path runmode ip_limit web_host web_username web_password web_port 
export http_proxy_ip http_proxy_port https_proxy_port bridge_ip bridge_port 
export public_vkey auth_key auth_crypt_key


nps_gen_conf() {

	if [ ! -d "$target_conf_dir" ]; then
		mkdir -p "$target_conf_dir"
	fi
	
	[ -f $target_conf_path ] && rm $target_conf_path
	echo "appname = nps" >$target_conf_path
	echo "runmode=${runmode}" >>$target_conf_path

	if [ "$http_proxy_ip" = "" ]; then
		echo "http_proxy_ip=0.0.0.0" >>$target_conf_path
	else
		echo "http_proxy_ip=${http_proxy_ip}" >>$target_conf_path
	fi

	echo "http_proxy_port=${http_proxy_port}" >>$target_conf_path
	echo "https_proxy_port=${https_proxy_port}" >>$target_conf_path
	echo "https_just_proxy=true" >>$target_conf_path
	echo "https_default_cert_file=conf/server.pem" >>$target_conf_path
	echo "https_default_key_file=conf/server.key" >>$target_conf_path
	echo "bridge_type=${bridge_type}" >>$target_conf_path
	echo "bridge_port=${bridge_port}" >>$target_conf_path
	echo "bridge_ip=${bridge_ip}" >>$target_conf_path
	echo "public_vkey=${public_vkey}" >>$target_conf_path
	echo "log_level=${log_level}" >>$target_conf_path
	echo "log_path=${log_path}" >>$target_conf_path
	echo "ip_limit=${ip_limit}" >>$target_conf_path
	echo "web_host=${web_host}" >>$target_conf_path
	echo "web_username=${web_username}" >>$target_conf_path
	echo "web_password=${web_password}" >>$target_conf_path
	echo "web_port=${web_port}" >>$target_conf_path
	echo "web_ip=${web_ip}" >>$target_conf_path
	echo "auth_key=${auth_key}" >>$target_conf_path
	echo "auth_crypt_key=${auth_crypt_key}" >>$target_conf_path
	
	if [ ! "$(readlink $nps_def_conf_path)" = "$target_conf_path" ] \
	&& [ ! "$nps_def_conf_path" = "$target_conf_path" ]; then
		rm $nps_def_conf_path
		ln -s $target_conf_path $nps_def_conf_path
	fi
}

nps_header() {	
	config_get Enabled $1 enabled

	config_get log_level $1 log_level
	config_get log_path $1 log_path
	config_get runmode $1 runmode
	config_get ip_limit $1 ip_limit
	config_get web_host $1 web_host
	config_get web_username $1 web_username
	config_get web_password $1 web_password
	config_get web_port $1 web_port
	config_get http_proxy_ip $1 http_proxy_ip
	config_get http_proxy_port $1 http_proxy_port
	config_get https_proxy_port $1 https_proxy_port
	config_get bridge_ip $1 bridge_ip
	config_get bridge_port $1 bridge_port
	config_get public_vkey $1 public_vkey
	config_get auth_key $1 auth_key
	config_get auth_crypt_key $1 auth_crypt_key
}

start_service() {
	config_load nps
	config_foreach nps_header nps
	if [ "$Enabled" = "1" ];then
		nps_gen_conf
		logger -t FRPS 'Starting Nps Server service' 
		#service_start /usr/bin/nps start
		procd_open_instance
		procd_set_param command /usr/bin/nps -log file
		procd_set_param file $target_conf_path
		procd_set_param respawn
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_close_instance
	fi
}

service_triggers()
{
        procd_add_reload_trigger "nps" 
}

reload_service()
{
        echo "restarting nps service"
        stop
        start
}
